package com.dxysun.springboot.filter;

import com.alibaba.fastjson.JSONObject;
import com.dxysun.springboot.wrapper.CustomizeResponseWrapper;
import com.dxysun.springboot.wrapper.MyHttpServletRequestWrapper;
import com.dxysun.springboot.wrapper.ResponseWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.util.ContentCachingResponseWrapper;

import java.io.IOException;
import java.util.Map;
import org.apache.commons.io.IOUtils;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@WebFilter
//@Order(201)
public class LogFilter implements Filter
{
	private static final Log logger = LogFactory.getLog(LogFilter.class);

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException
	{
		String msg = servletRequest.getParameter("msg");
		logger.info("LogFilter 请求前,msg" + msg);
		HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
		HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

		MyHttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper(httpServletRequest);
//		CustomizeResponseWrapper responseWrapper = new CustomizeResponseWrapper(httpServletResponse);
		ResponseWrapper responseWrapper = new ResponseWrapper(httpServletResponse);
		ContentCachingResponseWrapper contentCachingResponseWrapper = new ContentCachingResponseWrapper(httpServletResponse);

//		requestWrapper.setParameter("msg", "e25hbWU6InF3ZXJ0In0=");
//		chain.doFilter(servletRequestWrapper, responseWrapper);
//		chain.doFilter(requestWrapper, responseWrapper);
//		chain.doFilter(requestWrapper, contentCachingResponseWrapper);
		chain.doFilter(httpServletRequest, httpServletResponse);


//		msg = requestWrapper.getParameter("msg");
		int statusCode = httpServletResponse.getStatus();

		logger.info("LogFilter 请求后,statusCode" + statusCode);

//		processResponse(responseWrapper);
	}

	private void processContentCachingResponseWrapper(ContentCachingResponseWrapper contentCachingResponseWrapper ) throws IOException
	{
		String context = new String(contentCachingResponseWrapper.getContentAsByteArray());
		logger.info("LogFilter 请求后,processContentCachingResponseWrapper:" + context);
		JSONObject resultJson = new JSONObject();
		resultJson.put("code", "ok");
		resultJson.put("msg", "success");
		resultJson.put("result", context);
//		IOUtils.
//		contentCachingResponseWrapper.
		contentCachingResponseWrapper.getContentInputStream();
		contentCachingResponseWrapper.copyBodyToResponse();
	}

	private void processResponse(ResponseWrapper wrapper ) throws IOException
	{
//		String content = responseWrapper.getTextContent();
		String content = wrapper.getResponseData();

		logger.info("原返回值:" + content);

		JSONObject resultJson = new JSONObject();
		resultJson.put("code", "ok");
		resultJson.put("msg", "success");
		resultJson.put("result", content);

		/* httpServletResponse.setContentLength(resultJson.toJSONString().length());
		httpServletResponse.setContentType("application/json;charset=utf-8");
		httpServletResponse.setStatus(200);
		httpServletResponse.getOutputStream().write(resultJson.toJSONString().getBytes());*/

		String result = resultJson.toJSONString();
		logger.info("===返回值====" + result);
		wrapper.writeResponseBody(result);
		//对result进行一些处理（比如先把json字符串转换为map，然后进行修改，再转换为json字符串）
	}
}
